﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Manage.Model;
using Newtonsoft.Json;

namespace Manage.Basic
{
    public partial class FrmAdminMgr : Form
    {
        public FrmAdminMgr()
        {
            InitializeComponent();
        }
        private int Sid;
        private int SState;
        private string SName;
        private int SMCid;
        private string SLoginName;
        /// <summary>
        /// 载入窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAdminMgr_Load(object sender, EventArgs e)
        {
            try
            {
                cbxCompany.DataSource = null;
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                RetModel ret = Share.PostData("51API/Company/GetCompanyList", dc, null);
                if (ret.Success)
                {
                    ManagerCompany[] rmc = JsonConvert.DeserializeObject<ManagerCompany[]>(ret.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("CName");
                    for (int i = 0; i < rmc.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = rmc[i].id;
                        dr["CName"] = rmc[i].CName;
                        dt.Rows.Add(dr);
                    }
                    cbxCompany.DataSource = dt;
                    cbxCompany.ValueMember = "id";
                    cbxCompany.DisplayMember = "CName";
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                ManagerModel mm = new ManagerModel();
                mm.MLoginName = tbxLoginName.Text.Trim();
                mm.MName = tbxName.Text.Trim();
                mm.MCompanyID = Convert.ToInt32(cbxCompany.SelectedValue);
                mm.MPassWord = CryptUtil.Crypt.MD32Crypt("123456");
                RetModel ret = Share.PostData("51API/Manage/ManagerAdd", dc, JsonConvert.SerializeObject(mm));
                if (ret.Success)
                {
                    MessageBox.Show("添加成功！");
                    barButtonItem4_ItemClick(sender, e);
                    tbxLoginName.Text = "";
                    tbxName.Text = "";
                    cbxCompany.SelectedValue = 0;
                    
                }
                else
                {
                    MessageBox.Show("添加失败！\r\n错误代码:" + ret.ErrCode + "\r\n错误信息：" + ret.ErrInfo);
                    barButtonItem4_ItemClick(sender, e);
                    tbxLoginName.Text = "";
                    tbxName.Text = "";
                    cbxCompany.SelectedValue = 0;
                   
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 启用/禁用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (true)
                {
                    Dictionary<string, string> dc = new Dictionary<string, string>();
                    dc.Add("Token", Share.MoM.Token);
                    dc.Add("id", Sid.ToString());
                    RetModel ret = Share.PostData("51API/Manage/ManagerState", dc, null);
                    if (ret.Success)
                    {
                        MessageBox.Show("更改状态完成！");
                        barButtonItem4_ItemClick(sender, e);
                        tbxLoginName.Text = "";
                        tbxName.Text = "";
                        cbxCompany.SelectedValue = 0;
                        

                    }
                    else
                    {
                        MessageBox.Show("更改失败！\r\n错误代码:" + ret.ErrCode + "\r\n错误信息：" + ret.ErrInfo);
                        barButtonItem4_ItemClick(sender, e);
                        tbxLoginName.Text = "";
                        tbxName.Text = "";
                        cbxCompany.SelectedValue = 0;
                        
                    }
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                ManagerModel mm = new ManagerModel();
                mm.id = Sid;
                mm.MCompanyID = Convert.ToInt32(cbxCompany.SelectedValue);
                mm.MLoginName = tbxLoginName.Text.Trim();
                mm.MName = tbxName.Text.Trim();
                RetModel ret = Share.PostData("51API/Manage/ManagerEdit", dc,JsonConvert.SerializeObject(mm));
                if (ret.Success)
                {
                    MessageBox.Show("修改成功！");
                    barButtonItem4_ItemClick(sender, e);
                    tbxLoginName.Text = "";
                    tbxName.Text = "";
                    cbxCompany.SelectedValue = 0;
                    
                }
                else
                {
                    MessageBox.Show("修改失败！\r\n错误代码:" + ret.ErrCode + "\r\n错误信息：" + ret.ErrInfo);
                    barButtonItem4_ItemClick(sender, e);
                    tbxLoginName.Text = "";
                    tbxName.Text = "";
                    cbxCompany.SelectedValue = 0;
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                gridControl1.DataSource = null;
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                RetModel ret = Share.PostData("51API/Manage/GetManagerList", dc, null);
                if (ret.Success)
                {
                    ManagerModel[] Rmm = JsonConvert.DeserializeObject<ManagerModel[]>(ret.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("登录名");
                    dt.Columns.Add("姓名");
                    dt.Columns.Add("状态");
                    dt.Columns.Add("MCompanyid");
                    for (int i = 0; i < Rmm.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = Rmm[i].id;
                        dr["登录名"] = Rmm[i].MLoginName;
                        dr["姓名"] = Rmm[i].MName;
                        if (Rmm[i].State == 0)
                            dr["状态"] = "禁用";
                        else
                            dr["状态"] = "启用";
                        dr["MCompanyid"] = Rmm[i].MCompanyID;
                        dt.Rows.Add(dr);
                    }
                    gridControl1.DataSource = dt;
                    gridView1.Columns["id"].Visible = false;
                    gridView1.Columns["MCompanyid"].Visible = false;
                }
                else
                {
                    MessageBox.Show("查询失败！\r\n错误代码:" + ret.ErrCode + "\r\n错误信息：" + ret.ErrInfo);
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 选中列表中的行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                Sid = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["id"]));
                SName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["姓名"]).ToString();
                SMCid = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["MCompanyid"]));
                if (gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["状态"]).ToString() == "禁用")
                    SState = 0;
                else
                    SState = 1;
                SLoginName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["登录名"]).ToString();
                tbxLoginName.Text = SLoginName;
                tbxName.Text = SName;
                cbxCompany.SelectedValue = SMCid;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
